package com.gitee.fantaros.util;

import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.core5.http.ClassicHttpResponse;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import java.io.InputStream;
import java.util.function.BiConsumer;

public class HtmlUtil {

    public static void analyzeUrl(String url, BiConsumer<ClassicHttpResponse, Document> callback) throws Exception {
        try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
            HttpGet getMethod = new HttpGet(url);
            httpClient.execute(getMethod, response -> {
                Document document = Jsoup.parse(response.getEntity().getContent(), "UTF-8", url);
                callback.accept(response, document);
                return response;
            });
        }
    }

}
